package DAL;

import Entities.TblSubject;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class SubjectDAL {

    private ResultSet objResult;
    public List<TblSubject> list;

    public SubjectDAL() {
    }

    public boolean add(TblSubject subject) {
        int result = 0;
        String[] params = new String[4];
        params[0] = subject.getSubjectName();
        params[1] = String.valueOf(subject.getSemesterId());
        params[2] = String.valueOf(subject.getMarkMax());
        params[3] = String.valueOf(subject.getMarkMin());

        try {
            result = SQLHelper.executeUpdate("procInsertSubject", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblSubject subject) {
        int result = 0;
        String[] params = new String[5];
        params[0] = String.valueOf(subject.getSubjectId());
        params[1] = subject.getSubjectName();
        params[2] = String.valueOf(subject.getSemesterId());
        params[3] = String.valueOf(subject.getMarkMax());
        params[4] = String.valueOf(subject.getMarkMin());

        try {
            result = SQLHelper.executeUpdate("procUpdateSubject", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblSubject subject) {
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(subject.getSubjectId());

        try {
            result = SQLHelper.executeUpdate("procDeleteSubject", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblSubject> getAll() {
        list = new ArrayList<TblSubject>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllSubject", null);
            while (objResult.next()) {
                TblSubject subject = new TblSubject();
                subject.setSubjectId(objResult.getInt("SubjectId"));
                subject.setSubjectName(objResult.getString("SubjectName"));
                subject.setSemesterId(objResult.getInt("SemesterId"));
                subject.setMarkMax(objResult.getFloat("MarkMax"));
                subject.setMarkMin(objResult.getFloat("MarkMin"));

                list.add(subject);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblSubject getById(int id) {
        TblSubject subject = new TblSubject();
        try {
            objResult = SQLHelper.executeQuery("procGetSubjectById", null);
            while (objResult.next()) {
                subject.setSubjectId(objResult.getInt("SubjectId"));
                subject.setSubjectName(objResult.getString("SubjectName"));
                subject.setSemesterId(objResult.getInt("SemesterId"));
                subject.setMarkMax(objResult.getFloat("MarkMax"));
                subject.setMarkMin(objResult.getFloat("MarkMin"));
            }

            return subject;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public List<TblSubject> getSubjectBySemesterId(int id) {
        list = new ArrayList<TblSubject>();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetSubjectBySemesterId", params);
            while (objResult.next()) {
                TblSubject subject = new TblSubject();
                subject.setSubjectId(objResult.getInt("SubjectId"));
                subject.setSubjectName(objResult.getString("SubjectName"));
                subject.setSemesterId(objResult.getInt("SemesterId"));
                subject.setMarkMax(objResult.getFloat("MarkMax"));
                subject.setMarkMin(objResult.getFloat("MarkMin"));

                list.add(subject);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
